<?php

namespace app\admin\services;

use think\App;
use think\Auth;
use think\Service;
use think\facade\Session;
use think\facade\Request;
use app\admin\model\AuthgroupModel;
use app\admin\model\AuthruleModel;

class Authrule extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        global $authstate;
        $username = Session::get('username', 'admin');
        $uid = Session::get('uid', 'admin');
        if (!$username) {
            $this->redirect('admin/inout/login');
        }
        $pathinfo = '/'.Request::pathinfo();
        $auth = new Auth();
        $userauth = $auth->check($pathinfo, $uid);
        if (!$userauth) {
            $authstate = $this->_getResponse("AUTH_EORRE");
        }
        parent::__construct($request, $app);
        $this->AuthgroupModel = new AuthgroupModel;
        $this->AuthruleModel = new AuthruleModel;
    }


    /**
     * @param 管理角色列表
     */
    public function lorus(){
        $ruslt = $this->AuthgroupModel->lores();
        return $ruslt;
    }

    public function lorefk(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        if(!empty($post['title'])){
            $where = [['title','like','%'.$post['title'].'%']];
        }else{
            $where=[];
        }
        $ruslt = $this->AuthgroupModel->lroel($where,$page,$limit);
        $counts = $this->AuthgroupModel->county();
        $array=[];
        foreach ($ruslt as $value){
            $value['stuts'] = disa($value['status']);
            $array[] = $value;
        }
        if(empty($ruslt)){
            $data['rel'] = false;
            $data['msg'] = "暂无数据";
        }else{
            $data['rel'] = true;
            $data['msg'] = "获取成功";
        }
        $data['list'] = $array;
        $data['count'] = $counts;
        return $data;
    }


    /**
     * @param 添加角色
     */
    public function addtit(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if($post['title']==''){
            return $this->_getResponse("PARAM_EORRE");
        }
        $weiyi = $this->AuthgroupModel->titleone();
        $array = [];
        foreach ($weiyi as $value){
            $array[] = $value['title'];
        }
        if(in_array($post['title'],$array)){
            return $this->_getResponse("ROLE_ERROR");
        }
        $data = [
            'title'=> $post['title']
        ];
        $rusetl = $this->AuthgroupModel->addction($data);
        if(!$rusetl){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("USER_ADD");
    }


    /**
     * @param 修改角色
     */
    public function editos(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
       if($post['title']==''){
           return $this->_getResponse("PARAM_EORRE");
       }
        $where = [['id','eq',$post['roleid']]];
        $data = [
            'title'=> $post['title']
        ];
        $rulet = $this->AuthgroupModel->editction($where,$data);
        if(!$rulet){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 禁用角色
     */
    public function examine(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $where = [['id','eq',$post['id']]];
        $rustl = $this->AuthgroupModel->exbut($where);
        if(!$rustl){
            return $this->_getResponse("NUKNOW_EORRE");
        }
        $status = $rustl['status'];
        if($status==1){
            $data = ['status'=> 2];
        }else{
            $data = ['status'=> 1];
        }
        $listos = $this->AuthgroupModel->eidtion($where,$data);
        if(!$listos){
            return $this->_getResponse("CAOZUO_ERROR");
        }

        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 删除角色
     */
    public function delect(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }

        $post = Request::param();
        if(!$post['id']){
            return $this->_getResponse("PARAM_EORRE");
        }
        $id = $post['id'];
        $ruledt = $this->AuthgroupModel->delect($id);
        if(!$ruledt){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("DELETE_DATE");
    }


    /**
     * @param 获取当前角色ID
     */
    public function authid(){
        $rulstr = Request::param();
        $data = $this->AuthgroupModel->Authos($rulstr['id']);
        return $data;
    }


    /**
     * @param 获取权限
     */
    public function authOne(){
        $data = $this->AuthruleModel->Auths();
        return $data;
    }


    /**
     * @param 修改权限
     */
    public function alledit(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $post = $post['data'];
        $authid = $post['roleid'];
        $auth = $post['authid'];
        $auth = explode(';',$auth);
        $auth = implode(",",$auth);
        $data = [
            'rules' => $auth
        ];
        $where = [['id','eq',$authid]];
        $list = $this->AuthgroupModel->saveauth($where,$data);
        if(!$list){
            return $this->_getResponse("CAOZUO_ERROR");
        }

        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "ROLE_ERROR" => ["status" => 7, "msg" => "该角色已经存在", "data" => $data]
        ];
        return $maps[$key];
    }

}